/**
 * Software License, Version 1.0
 * 
 * Copyright 2003 The Trustees of Indiana University.  All rights reserved.
 * 
 *
 *Redistribution and use in source and binary forms, with or without 
 *modification, are permitted provided that the following conditions are met:
 *
 *1) All redistributions of source code must retain the above copyright notice,
 * the list of authors in the original source code, this list of conditions and
 * the disclaimer listed in this license;
 *2) All redistributions in binary form must reproduce the above copyright 
 * notice, this list of conditions and the disclaimer listed in this license in
 * the documentation and/or other materials provided with the distribution;
 *3) Any documentation included with all redistributions must include the 
 * following acknowledgement:
 *
 *"This product includes software developed by the Community Grids Lab. For 
 * further information contact the Community Grids Lab at 
 * http://communitygrids.iu.edu/."
 *
 * Alternatively, this acknowledgement may appear in the software itself, and 
 * wherever such third-party acknowledgments normally appear.
 * 
 *4) The name Indiana University or Community Grids Lab or NaradaBrokering, 
 * shall not be used to endorse or promote products derived from this software 
 * without prior written permission from Indiana University.  For written 
 * permission, please contact the Advanced Research and Technology Institute 
 * ("ARTI") at 351 West 10th Street, Indianapolis, Indiana 46202.
 *5) Products derived from this software may not be called NaradaBrokering, 
 * nor may Indiana University or Community Grids Lab or NaradaBrokering appear
 * in their name, without prior written permission of ARTI.
 * 
 *
 * Indiana University provides no reassurances that the source code provided 
 * does not infringe the patent or any other intellectual property rights of 
 * any other entity.  Indiana University disclaims any liability to any 
 * recipient for claims brought by any other entity based on infringement of 
 * intellectual property rights or otherwise.  
 *
 *LICENSEE UNDERSTANDS THAT SOFTWARE IS PROVIDED "AS IS" FOR WHICH NO 
 *WARRANTIES AS TO CAPABILITIES OR ACCURACY ARE MADE. INDIANA UNIVERSITY GIVES
 *NO WARRANTIES AND MAKES NO REPRESENTATION THAT SOFTWARE IS FREE OF 
 *INFRINGEMENT OF THIRD PARTY PATENT, COPYRIGHT, OR OTHER PROPRIETARY RIGHTS. 
 *INDIANA UNIVERSITY MAKES NO WARRANTIES THAT SOFTWARE IS FREE FROM "BUGS", 
 *"VIRUSES", "TROJAN HORSES", "TRAP DOORS", "WORMS", OR OTHER HARMFUL CODE.  
 *LICENSEE ASSUMES THE ENTIRE RISK AS TO THE PERFORMANCE OF SOFTWARE AND/OR 
 *ASSOCIATED MATERIALS, AND TO THE PERFORMANCE AND VALIDITY OF INFORMATION 
 *GENERATED USING SOFTWARE.
 */
package cgl.narada.service;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

public class ServiceProperties {
	private Properties properties;

	private Properties defaultProperties;

	private static ServiceProperties instance = new ServiceProperties();

	private String configFileLocation;

	private int entityId;

	private static String moduleName = "ServiceProperties: ";

	protected ServiceProperties() {
		loadDefaultProperties();
		properties = new Properties(defaultProperties);
	}
	

	/**
	 * Retrieve the entity identifier
	 * @return The entity id.
	 */
	public int getEntityId() {
		return entityId;
	}
	
	/**
	 * Get the configuration file location.
	 * @return the configuration file location.
	 */
	public String getConfigFileLocation() {
		return configFileLocation;
	}
	
	

	/**
	 * Check to see if a certain property exists.
	 * 
	 * @param propertyName
	 *           The property to check for.
	 */
	public boolean hasProperty(String propertyName) {
		if (properties.containsKey(propertyName)) {
			return true;
		}
		return false;
	}

	/**
	 * Retrieve a certain property. This will return a NULL if the property is
	 * not found.
	 * 
	 * @param propertyName
	 *           The property to retrieve.
	 */
	public String getProperty(String propertyName) {
		return properties.getProperty(propertyName);
	}
	
	/**
	 * Searches for the property with the specified key in this property list. If the
	 * key is not found in this property list, the default property list, and its 
	 * defaults, recursively, are then checked. The method returns the default value 
	 * argument if the property is not found.
	 * @param propertyName Checks the specified property
	 * @param defaultValue Default value for this property.
	 * @return
	 */
	public String getProperty(String propertyName, String defaultValue) {
		return properties.getProperty(propertyName, defaultValue);
	}

	
	/**
	 * Initialize the service properties based on the specified
	 * configFileLocation and the specified entityId.
	 * 
	 * @param entityId
	 *           Idenetifier for the entity.
	 *           
	 * @param configFileLocation
	 *           Location of the configuration file
	 * 
	 * @return true if the intialization was successful and false if NOT.
	 */
	protected boolean initialize(int entityId, String configFileLocation) {
		if (configFileLocation == null) {
			System.out.println(moduleName
					+ "The specified configFile location is NULL");
		}
		this.entityId = entityId;
		this.configFileLocation = configFileLocation;

		boolean loadingSuccessful = loadProperties();
		if (!loadingSuccessful) {
			return false;
		}
		return true;
	}

	/**
	 * Loads the properties form properties file.
	 * 
	 * @return boolean
	 */
	private boolean loadProperties() {
		try {
			properties.load(new FileInputStream(configFileLocation));
		} catch (IOException ex) {
			System.out.println(moduleName + "Problems loading file: "
					+ configFileLocation + "\n" + ex.toString());
			return false;
		}
		return true;
	}
	
	

	/**
	 * Default property values are set.
	 * 
	 * @return Properties - Default properties.
	 */
	private void loadDefaultProperties() {
		defaultProperties = new Properties();
		defaultProperties.setProperty("NB_HOME", "C:/NB/NaradaBrokering");
		defaultProperties.setProperty("FragmentationDirectory",
				"C:/TempFiles/tmpFiles/fragment");
		defaultProperties.setProperty("CoalescingDirectory",
				"C:/TempFiles/tmpFiles/coalesce");
		defaultProperties.setProperty("SecurityKeyStore",
				"C:/SecurityStores/keystore");
		defaultProperties.setProperty("SecurityTrustStore",
				"C:/SecurityStores/truststore");
		defaultProperties.setProperty("SecurityProvider", "CryptixCrypto");
		defaultProperties
				.setProperty(
						"NTP_Servers",
						"129.6.15.28,129.6.15.29,132.163.4.101,132.163.4.102,132.163.4.103,192.43.244.18");
		defaultProperties.setProperty("NTP_Interval", "2000");
		defaultProperties.setProperty("NTP_Debug", "OFF");
		defaultProperties.setProperty("Storage_Type", "db");
		defaultProperties.setProperty("Database_JDBC_Driver",
				"org.gjt.mm.mysql.Driver");
		defaultProperties
				.setProperty("Database_ConnectionProvider", "jdbc:mysql");
		defaultProperties.setProperty("Database_ConnectionHost", "localhost");
		defaultProperties.setProperty("Database_ConnectionPort", "3306");
		defaultProperties.setProperty("Database_ConnectionDatabase",
				"NbPersistence");
		defaultProperties.setProperty("FileStorage_BaseDirectory",
				"C:/NBStorage/filebased/persistent");
		defaultProperties.setProperty("TOB_MaximumTotalBufferSize", "2500000");
		defaultProperties
				.setProperty("TOB_MaximumNumberOfBufferEntries", "10000");
		defaultProperties.setProperty("TOB_MaximumBufferEntryDuration", "50000");
		defaultProperties.setProperty("TOB_BufferReleaseFactor", "0.8");
		defaultProperties.setProperty("BDNDiscoveryList", "http://www.gridservicelocator.org/");
		defaultProperties.setProperty("MulticastGroupHost", "224.224.224.224");
		defaultProperties.setProperty("MulticastGroupPort", "0");
	}

}
